package com.lpit.controller;

import com.lpit.constant.Constant;
import com.lpit.pojo.SysAdmin;
import com.lpit.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/start")
    public String start(){
        return "登录.html";
    }

    @RequestMapping("/admin/login")
    public String login(SysAdmin admin, HttpSession session){
        SysAdmin adminLogin = adminService.adminLogin(admin);
        System.err.println(adminLogin);
        if (null != adminLogin) session.setAttribute(Constant.ADMIN,adminLogin);
        return "用户列表.html";
    }
    @RequestMapping("/admin/logout")
    public String logout(HttpSession httpSession){
        /*清除session*/
        httpSession.invalidate();
        return Constant.HTML_LOGIN_URL;
    }
    /*
     * 测试注册功能是否可以添加成功
     * 测试结果：已完成 注册功能
     * */

    @RequestMapping("/enroll")
    @ResponseBody
    public String enroll(SysAdmin admin){
        /*
         * admin：一个用户表的实体类
         * */
        /*
            测试临时代码
            Admin admin = new Admin(null,"1234","456",null,null);
        */
        String setAdminMapper = adminService.setAdminMapper(admin);
        return setAdminMapper;

    }
    /*修改admin用户密码*/
    @RequestMapping("/enroll/testReviseAdmin")
    @ResponseBody
    public String testReviseAdmin(String username,String admin,String reviseAdmin){
        /*
        测试代码
        String username = "1234";
        String admin = "111";
        String reviseAdmin = "112";
        */
        /*调用接口 给出账号，原密码，和新密码 、实现密码修改*/
        String setAdminMapper = adminService.reviseAdmin(username,admin,reviseAdmin);
        return setAdminMapper;
    }
//    查询管理员用户
    @RequestMapping("/inquire/queryManagement")
    @ResponseBody
    public List<SysAdmin> inquire(SysAdmin sysAdmin){
        List<SysAdmin> inquire = adminService.inquire(sysAdmin);
        System.out.println("inquire = " + inquire);
        return inquire;
    }

//    删除管理员用户
    @RequestMapping("/Delete/DeleteManagement")
    @ResponseBody
    public String DeleteManagement(Integer adminId){
        System.out.println(adminId);
        boolean b = adminService.removeById(adminId);
        return adminService.removeById(adminId) ? "删除成功" : "删除失败";
    }
}


